#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMessageBox>



void Widget::shenfu()
{
  //当模式为元素大师的计分方式
  if(0==gamewin->pattern)
  {
    for(int i=0;i<5;i++)
    {
      for(int j=0;j<5;j++)
      {
        if(0==gamewin->p[i][j].pin_state)
        {
          return;
        }
      }
    }
    if(gamewin->fen1>gamewin->fen2)
    {
      QMessageBox::about(NULL, "恭喜你获胜", "你赢了");
    }
    else
    {
      QMessageBox::about(NULL, "你输了", "你输了");
    }
  }

  //当模式为四角逐鹿的计分方式
  if(1==gamewin->pattern)
  {
    for(int i=0;i<4;i+=4)
    {
      for(int j=0;j<4;j+=4)
      {
        if(0==gamewin->p[i][j].pin_state)
        {
          return;
        }
      }
    }

    if(gamewin->fen1>gamewin->fen2)
    {
      QMessageBox::about(NULL, "恭喜你获胜", "你赢了");
    }
    else
    {
      QMessageBox::about(NULL, "你输了", "你输了");
    }
  }

  //当模式为全面战争的计分方式
  if(2==gamewin->pattern)
  {
    for(int i=0;i<5;i++)
    {
      for(int j=0;j<5;j++)
      {
        if(0==gamewin->p[i][j].pin_state)
        {
          return;
        }
      }
    }

    if(gamewin->fen1>gamewin->fen2)
    {
      QMessageBox::about(NULL, "恭喜你获胜", "你赢了");
    }
    else
    {
      QMessageBox::about(NULL, "你输了", "你输了");
    }
  }

  gamewin->chu();
}

Widget::Widget(QWidget *parent) :
                                  QWidget(parent),
                                  ui(new Ui::Widget)
{
  ui->setupUi(this);
  tcpSocket=new QTcpSocket(this);
  sinwin->show();
  //gamewin->show();
  //登录界面登录按钮
  connect(sinwin->sin_button,&QPushButton::clicked,[=]{
    //tcpSocket->connectToHost(QString("cn-cd-dx.sakurafrp.com"),qint16(38656));
    tcpSocket->connectToHost(QString("cn-fz-dx.sakurafrp.com"),qint16(53054));
    sinwin->close();
    gamewin->show();
  });

  //登录界面关闭按钮
  connect(sinwin->sin_close,&QPushButton::clicked,[=]{
    this->close();
  });




  connect(tcpSocket,&QTcpSocket::connected,[=]{
    qDebug()<<"连接成功";
    sinwin->number_line->setText(QString("连接成功"));
    sinwin->close();
    gamewin->show();
  });
  connect(tcpSocket,&QTcpSocket::disconnected,[=]{
    qDebug()<<"服务器已断开连接";
    sinwin->password_line->setText(QString("服务器已断开连接"));
    sinwin->number_line->setText(QString("服务器人数已满"));
  });

  for(int i=0;i<5;i++)
  {
    for(int j=0;j<5;j++)
    {
      connect(&gamewin->buton[i][j],&QPushButton::clicked,[=]{
          QString str=QString("%1 %2").arg(i).arg(j);
          tcpSocket->write(str.toUtf8().data());//发送消息
          gamewin->chongzhitouzi->close();//关闭重置骰子和跳过
          gamewin->guo->close();
          gamewin->zhitouzi->show();//打开掷骰子
          gamewin->zhitouzi->setEnabled(false);
          gamewin->p[i][j].pin_state=Pin::Challenge;//这个点归己方
          for(int i=0;i<5;i++)//所有按钮不能点，轮到对方点
          {
            for(int j=0;j<5;j++)
            {
              gamewin->buton[i][j].setEnabled(false);
            }
          }
          gamewin->zuofen();

      });
    }
  }


  connect(gamewin->renshu,&QPushButton::clicked,[=]{
    tcpSocket->write("认输");
    gamewin->chongzhitouzi->close();
    gamewin->guo->close();
    gamewin->zhitouzi->setEnabled(false);
    for(int i=0;i<5;i++)
    {
      for(int j=0;j<5;j++)
      {
        gamewin->buton[i][j].setEnabled(false);
        gamewin->p[i][j].pin_state=Pin::unmanned;
      }
    }
    QMessageBox::critical(NULL, "你输了", "对方获胜", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
  });

  connect(gamewin->guo,&QPushButton::clicked,[=]{
    tcpSocket->write("过");

    gamewin->chongzhitouzi->close();
    gamewin->guo->close();
    gamewin->zhitouzi->show();
    gamewin->zhitouzi->setEnabled(false);

  });


  connect(tcpSocket,&QTcpSocket::readyRead,[=]{
    QString str=tcpSocket->readAll();
    if("过"==str)
    {
      gamewin->zhitouzi->setEnabled(true);
    }
    else if("认输"==str)
    {
      gamewin->chongzhitouzi->close();
      gamewin->guo->close();
      gamewin->zhitouzi->setEnabled(false);
      for(int i=0;i<5;i++)
      {
        for(int j=0;j<5;j++)
        {
          gamewin->buton[i][j].setEnabled(false);
          gamewin->p[i][j].pin_state=Pin::unmanned;
        }
      }
      QMessageBox::about(NULL, "恭喜你获胜", "对方认输");
    }
    else if ("开始"==str)
    {
      gamewin->zhitouzi->setEnabled(true);
      int a[5][5]={0};
      QString str=QString("k%1%2")
                        .arg(gamewin->state)
                        .arg(gamewin->pattern);
      for(int i=0;i<5;i++)
      {
        for(int j=0;j<5;j++)
        {
          a[i][j]=gamewin->p[i][j].pin_season;
          str=QString("%1%2").arg(str).arg(a[i][j]);
        }
      }
      tcpSocket->write(str.toUtf8().data());
    }
    else if("j"==str)
    {
      gamewin->state=(gamewin->state+1)%4;
      gamewin->update();
    }
    else
    {
      char *s=str.toUtf8().data();
      if(s[0]=='k')
      {
        gamewin->state=s[1]-48;
        gamewin->pattern=s[2]-48;
        for(int i=0;i<5;i++)
        {
          for(int j=0;j<5;j++)
          {
            gamewin->p[i][j]=s[i*5+j+3]-48;
          }
        }
        gamewin->tong();
        gamewin->update();

      }
      else
      {
        int i=s[0]-48;
        int j=s[2]-48;
        gamewin->zhitouzi->setEnabled(true);
        gamewin->p[i][j].pin_state=Pin::Challenged;
        gamewin->zuofen();
        gamewin->update();
      }
    }


  });

}

Widget::~Widget()
{
  delete ui;
}














